package com.zhuss.order.processor;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;

import com.zhuss.bean.bo.order.order.CareOrderBo;
import com.zhuss.bean.bo.order.order.WxCareOrderBo;
import com.zhuss.bean.unit.OrderTemplateParam5;
import com.zhuss.bean.vo.core.user.DetailUserVo;
import com.zhuss.bean.vo.order.order.CareOrderVo;
import com.zhuss.bean.vo.order.order.DetailOrderVo;
import com.zhuss.bean.vo.order.order.WxCareOrderVo;
import com.zhuss.common.AbstractProcessor;
import com.zhuss.common.Processor;
import com.zhuss.core.processor.UserRead;
import com.zhuss.db.entity.OpLog;
import com.zhuss.enums.EnumUtil;
import com.zhuss.enums.LogActionEnum;
import com.zhuss.enums.MoudleEnum;
import com.zhuss.enums.OrderStatus;
import com.zhuss.enums.微信返回的支付状态;
import com.zhuss.exception.DzcxException;
import com.zhuss.order.db.dao.ext.OrderDao;
import com.zhuss.order.db.dao.ext.OrderItemDao;
import com.zhuss.order.db.dao.ext.PrepayDao;
import com.zhuss.util.AliSmsService;
import com.zhuss.util.JsonUtil;

/**
 * 查询微信订单
 * 回写支付单状态 processor
 * @Author: zhushanshuo
 * @Date: 2019/9/24 19:54
 */
@Component("careOrderForUpdate")
public class CareOrderForUpdate extends AbstractProcessor<CareOrderVo, CareOrderBo> implements Processor<CareOrderVo, CareOrderBo> {

    @Autowired
    private OrderDao orderDao;
    
    @Autowired
    private OrderRead orderRead;
    
    @Autowired
    private UserRead userRead;
    
    @Autowired
    private OrderItemDao orderItemDao;
    
    @Autowired
    private PrepayDao prepayDao;
    
    @Autowired
    @Qualifier("wxCareOrderProcessor")
    private Processor<WxCareOrderVo, WxCareOrderBo> wxCareOrderProcessor;
    
    @Autowired
	private AliSmsService aliSmsService;
    
    /**
     * 校验
     */
    @Override
    public void check(CareOrderBo param) throws DzcxException {
    	
    }
    
    /**
     * 操作方法
     * @throws DzcxException 
     */
    @Override
    public CareOrderVo process(CareOrderBo param) throws DzcxException {
    	Long orderId = param.getOrderId();
    	DetailOrderVo order = orderRead.detail(orderId);
    	
    	WxCareOrderBo wxPayParam = new WxCareOrderBo();
    	wxPayParam.setConcurrentUserId(param.getConcurrentUserId());
    	wxPayParam.setOrderNo(order.getOrderNo());
    	WxCareOrderVo vo = wxCareOrderProcessor.process(wxPayParam);
    	String 微信状态名称 = vo.getTradeState();
    	String transactionId = vo.getTransactionId();
    	
    	Integer 微信返回的状态值 = EnumUtil.containsValue(微信返回的支付状态.class, 微信状态名称);
    	
    	Integer 库里的状态 = order.getStatus();
    	if (库里的状态 < 0 || 微信返回的状态值 == null) {
    		return new CareOrderVo(orderId, 微信返回的状态值, 库里的状态, "暂不更新状态");
    	}
    	
    	if (微信返回的支付状态.未支付.sameKey(微信返回的状态值)
    			|| 库里的状态.equals(微信返回的状态值)) {
    		return new CareOrderVo(orderId, 微信返回的状态值, 库里的状态, "暂不更新状态");
    	}
    	
    	if (库里的状态.equals(OrderStatus.已确认上车)) {
    		return new CareOrderVo(orderId, 微信返回的状态值, 库里的状态, "暂不更新状态");
    	}
    	
    	Date date = new Date();
    	orderDao.refreshStatus(orderId, 微信返回的状态值, transactionId, param.getConcurrentUserName(), param.getConcurrentUserId(), date);
    	orderItemDao.refreshStatus(orderId, 微信返回的状态值, param.getConcurrentUserName(), param.getConcurrentUserId(), date);
    	prepayDao.refreshStatus(orderId, 微信返回的状态值, param.getConcurrentUserName(), param.getConcurrentUserId(), date);
    	
    	if (微信返回的支付状态.支付成功.sameKey(微信返回的状态值)) {
    		List<Date> dateList = orderItemDao.getDateByOrderId(orderId);
    		OrderTemplateParam5 orderTemplateParam = new OrderTemplateParam5();
    		// orderTemplateParam.setDateList(dateList);
    		orderTemplateParam.setNum(order.getNum());
    		orderTemplateParam.setOnSiteName(order.getOnSiteName());
    		orderTemplateParam.setOffSiteName(order.getOffSiteName());
    		orderTemplateParam.setPersonNum(order.getPersonNum());
    		orderTemplateParam.setTotalPrice(order.getPrice() * order.getNum() * order.getPersonNum() / 100 + "");
    		orderTemplateParam.setCreator(order.getCreator());
    		orderTemplateParam.setOnSiteTime(order.getOnSiteTime());
    		orderTemplateParam.setOrderDate(dateList);
    		DetailUserVo user = (DetailUserVo) userRead.detail(order.getCreatorId());
    		aliSmsService.sendOrderMsg5(user.getUsername(), JsonUtil.toJson(orderTemplateParam));
    	}
    	
        return new CareOrderVo(orderId, 微信返回的状态值, 库里的状态, null);
    }
    
    /**
     * 操作日志
     */
	@Override
	public CareOrderVo log(CareOrderBo param) throws DzcxException {
		OpLog record = new OpLog();
    	record.setMoudle(MoudleEnum.订单.key());
    	record.setAction(LogActionEnum.回调支付单.key());
    	record.setTargetId(null);
    	record.setTargetName(param.getOrderId() + "");
    	record.setContent("参数: " + JsonUtil.toJson(param));
    	record.setReason(param.getExceptionReason());
    	
    	record.setCreatorId(param.getConcurrentUserId());
    	record.setCreator(param.getConcurrentUserName());
    	record.setCreateTime(new Date());
    	log(record);
    	return null;
	}
}
